/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package softclipping;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.Polygon;
import java.awt.RenderingHints;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import javax.swing.JPanel;

/**
 *
 * @author Nhat Lam
 */
public class panel extends JPanel{
    public void paint(Graphics g){
        //Fill Black Background
        g.setColor(Color.BLACK);
        g.fillRect(0, 0, 500, 500);
        
        /*
        g.setColor(Color.BLUE);
        Graphics2D g2d = (Graphics2D)g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);        
        g2d.fillOval(230, 150, 200, 200);
        g.setXORMode(Color.BLACK);
        g2d.setColor(Color.RED);
        g2d.fillRect(150, 50, 180, 180);
        int tx[] = new int[3];
        tx[0] = 190;
        tx[1] = 30;
        tx[2] = 350;
        int ty[] = new int[3];
        ty[0] = 140;
        ty[1] = 300;
        ty[2] = 300;
        Polygon triangle  = new Polygon(tx, ty, 3);
        g.setColor(Color.GREEN);
        g.fillPolygon(triangle);
        */

        //Draw Normal Oval
        g.setColor(Color.YELLOW);
        g.fillOval(30, 125, 200, 200);

        //Draw Smooth Oval
        Graphics2D g2d = (Graphics2D)g;
        GraphicsConfiguration gc = g2d.getDeviceConfiguration();
        BufferedImage img = gc.createCompatibleImage(500, 500, Transparency.TRANSLUCENT);
        g2d = img.createGraphics();
        g2d.setComposite(AlphaComposite.Clear);
        g2d.fillRect(0, 0, 400, 400);
        g2d.setComposite(AlphaComposite.Src);
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setColor(Color.YELLOW);
        g2d.fillOval(270, 125, 200, 200);
        g2d.setComposite(AlphaComposite.SrcAtop);
//        g2d.setPaint(new GradientPaint(0, 0, Color.GREEN, 0, 400, Color.BLUE));
        g2d.fillRect(0, 0, 400, 400);
        g2d.dispose();
        g.drawImage(img, 0, 0, null);
        g.setColor(Color.BLACK);
        g.drawString("Normal Draw", 90, 225);
        g.drawString("Smooth Draw", 330, 225);
        
    }
}
